package com.codingday.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TODONew extends Activity {

	Button buttonMapView = null;
	Button buttonSave = null;
	EditText editTextTitle = null;
	EditText editTextDesc = null;
	
	TextView tv_locationResult = null;
	
	double latitude;
	double longitude;
	int radius;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newtodo);
		
		latitude = 0.0;
		longitude = 0.0;
		radius = 0;
		
		buttonMapView = (Button) findViewById(R.id.ButtonSearchFromMap);
		buttonSave = (Button) findViewById(R.id.ButtonSave);
		editTextTitle = (EditText) findViewById(R.id.EditTextTitle);
		editTextDesc = (EditText) findViewById(R.id.EditTextDescription);
		tv_locationResult = (TextView) findViewById(R.id.newtodo_location_result);
		
		buttonSave.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Save Here
				// Go Back To Prev Activity

				if(check()){
					TODO todo = new TODO();
					Log.i("New TODO","TITLE : "+editTextTitle.getText().toString() + ":" + editTextTitle.getText().length());
					Log.i("New TODO","DESC  : "+editTextDesc.getText().toString() + ":" + editTextDesc.getText().length());

					todo.setTitle(editTextTitle.getText().toString());
					todo.setDesc(editTextDesc.getText().toString());
					todo.setLocation(latitude, longitude,radius);
					//todo.setRadius(300);
					//boolean saved = SaveManager.SaveTODO(todo);
					boolean saved = TODOListManager.addTODO(todo);
					if(saved){
						Log.i("New TODO","Save Success");
					}else{
						Log.i("New TODO","Save Failed");
					}
				}else{
					Toast.makeText(getApplicationContext(), "Wrong Input. Please Check Again", 1).show();
					return;
				}
				
				
				
				Log.i("New Todo", "Save Presssed");
				//Intent intent = new Intent(TODONew.this,GeoAlarm.class);
				//result.putExtra("result", true);
				//setResult(Activity.RESULT_OK, result);				
				//finish();
				//startActivity(intent);
				resetAll();
				GeoAlarm.getInstance().setCurrentTab(1);
				Toast.makeText(getApplicationContext(), "Quest saved", 1).show();
			}
			
		});
		
		
		buttonMapView.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(TODONew.this, MapLocPicker.class);
				startActivityForResult(intent, 2);
			}
			
		});
		
	}
	
	public void resetAll(){
		editTextTitle.setText("");
		editTextDesc.setText("");
		//buttonMapView.setText("Select Location");
		tv_locationResult.setText("");
		latitude = 0.0;
		longitude = 0.0;
		radius = 0;
	}
	
	public boolean check(){
		Log.i("New ToDo","Check Inputs");
		//Log.i();
		Log.i("New TODO",editTextTitle.getText().toString());
		if(latitude == 0.0 && longitude == 0.0){
			return false;
		}
		if(editTextTitle.getText().length() == 0){
			return false;
		}
		if(radius <= 0){
			return false;
		}
		return true;
	}
	
	

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Intent result = new Intent();
		result.putExtra("result", false);
		setResult(Activity.RESULT_OK, result);				
		finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		switch(requestCode){
		case 2:
			this.latitude = data.getDoubleExtra("location_latitude", 0);
			this.longitude = data.getDoubleExtra("location_longitude", 0);
			this.radius = data.getIntExtra("location_radius", 0);

			//Toast.makeText(getApplicationContext(), "Map Pocked ("+latitude+","+longitude+")", 1).show();
			//tv_locationResult.setText("[ "+latitude+" , "+longitude+" ]");
			buttonMapView.setText("[ "+latitude+" , "+longitude+" ]");
			
			
			break;
		default:
			Toast.makeText(getApplicationContext(), "Map Pocked default" + requestCode, 1).show();
			break;
		}
	}

	
	
}
